import { execute } from "../../database/connector";

import { ResearcherQueries } from "../queries/researcher.query";
import { IResearcher } from "../../models/researcher.model";

/**
 * gets active researcher
 */
export const getResearchers = async () => {
  return execute<IResearcher[]>(ResearcherQueries.GetResearchers, []);
};

/**
 * gets a researcher based on id provided
 */
export const getResearcherById = async (id: IResearcher['id']) => {
  return execute<IResearcher>(ResearcherQueries.GetResearcherById, [id]);
};

/**
 * adds a new active researcher record
 */
export const insertResearcher = async (researcher: IResearcher) => {
  const result = await execute<{ affectedRows: number }>(ResearcherQueries.AddResearcher, [
    [Object.values(researcher)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates researcher information based on the id provided
 */
export const updateResearcher = async (researcher: IResearcher) => {
  const result = await execute<{ affectedRows: number }>(ResearcherQueries.UpdateResearcherById, [researcher, researcher.id]);
  return result.affectedRows > 0;
};

/**
 * updates researcher information based on the id provided
 */
 export const deleteResearcher = async (id: IResearcher['id']) => {
  const result = await execute<{ affectedRows: number }>(ResearcherQueries.DeleteResearcherById, [
    id
  ]);
  return result.affectedRows > 0;
};